package com.ruoyi.demo.controller.demo;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.demo.domain.Address;
import com.ruoyi.demo.domain.GoodsDto;
import com.ruoyi.demo.domain.bo.GoodsBo;
import com.ruoyi.demo.domain.vo.GoodsVo;
import com.ruoyi.demo.mapper.GoodsDtoMapper;
import com.ruoyi.demo.mapper.GoodsTypeMapper;
import com.ruoyi.demo.service.IGoodsService;
import com.ruoyi.excel.service.IAsyncImportExportTaskService;
import com.ruoyi.system.domain.vo.SysUserExportVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 商品
 *
 * @author fjw
 * @date 2023-06-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/demo/goods")
public class GoodsController extends BaseController {

    private final IGoodsService iGoodsService;

    @Autowired
    private GoodsDtoMapper goodsDtoMapper;

    /**
     * 统计商品类型数
     */
    @SaCheckPermission("demo:goods:count")
    @GetMapping("/count")
    public TableDataInfo<GoodsDto> count() {
        List<GoodsDto> list = goodsDtoMapper.selectCountByType();
        System.out.println(list);
        return TableDataInfo.build(list);
    }


    /**
     * 查询商品列表
     */
    @SaCheckPermission("demo:goods:list")
    @GetMapping("/list")
    public TableDataInfo<GoodsVo> list(GoodsBo bo, PageQuery pageQuery) {
        return iGoodsService.queryPageList(bo, pageQuery);
    }

    private final IAsyncImportExportTaskService asyncImportExportTaskService;
    /**
     * 导出商品列表
     */
    @SaCheckPermission("demo:goods:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public R export(GoodsBo bo, HttpServletResponse response) {
        List<GoodsVo> list = iGoodsService.queryList(bo);
        Boolean result = asyncImportExportTaskService.addExport(list, GoodsVo.class, "水果数据");
        return result ? R.ok("已添加到我的任务，请查看"):R.fail("后端异常，请重试");
//        ExcelUtil.exportExcel(list, "商品", GoodsVo.class, response);
    }

    /**
     * 获取商品详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("demo:goods:query")
    @GetMapping("/{id}")
    public R<GoodsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iGoodsService.queryById(id));
    }

    /**
     * 新增商品
     */
    @SaCheckPermission("demo:goods:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody GoodsBo bo) {
        return toAjax(iGoodsService.insertByBo(bo));
    }

    /**
     * 修改商品
     */
    @SaCheckPermission("demo:goods:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody GoodsBo bo) {
        return toAjax(iGoodsService.updateByBo(bo));
    }

    /**
     * 删除商品
     *
     * @param ids 主键串
     */
    @SaCheckPermission("demo:goods:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iGoodsService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
